package com.java.zhuiyun.api.course_unit.service.impl;

import com.java.zhuiyun.api.course_unit.entity.CourseUnit;
import com.java.zhuiyun.api.course_unit.mapper.CourseUnitMapper;
import com.java.zhuiyun.api.course_unit.service.CourseUnitService;
import com.java.zhuiyun.api.file_manager.entity.FileManager;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CourseUnitServiceImpl
 * @Description TODO
 * @Author 杨友朋
 * @Date 2023/8/15 19:54
 **/
@Service
public class CourseUnitServiceImpl implements CourseUnitService {
    @Resource
    CourseUnitMapper courseUnitMapper;


    /**
    * @Author 杨友朋
    * @Description TODO 查询章节
    * @Date 2023/8/15 20:56
    * @Return
    * @param
    */
    @Override
    public PageResult selectUnit(Integer courseId, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        List<CourseUnit> courseUnits = courseUnitMapper.selectUnit(courseId);

        return MybatisPageHelper.getPageResult(courseUnits);
    }

    /**
    * @Author 杨友朋
    * @Description TODO 添加章节
    * @Date 2023/8/15 20:55
    * @Return
    * @param
    */
    @Override
    public CommonResult addUnit(CourseUnit courseUnit){
        //创建时间
        courseUnit.setCreateTime(new Date());
        //修改时间
        courseUnit.setUpdateTime(new Date());
        int i = courseUnitMapper.insertSelective(courseUnit);
        if (i>0){
            return CommonResult.ok();
        }
        return CommonResult.error(500,"添加失败");
    }

    /**
    * @Author MJL
    * @Description todo 章节内添加视频
    * @Date 2023/8/17 10:14
    * @Return  * @param null
    */
    @Override
    public Integer addFile(CourseUnit courseUnit) {
        return courseUnitMapper.addFile(courseUnit);
    }

    /**
    * @Author MJL
    * @Description todo 查询可以添加的视频
    * @Date 2023/8/17 14:29
    * @Return  * @param null
    */
    @Override
    public List<FileManager> selectFile(String fileType) {
        return courseUnitMapper.selectFile(fileType);
    }

    /**
    * @Author 杨友朋
    * @Description TODO 修改章节
    * @Date 2023/8/21 16:01
    * @Return
    * @param
    */
    @Override
    public CommonResult updateUnit(CourseUnit courseUnit) {
        if (courseUnit.getId()!=null&&courseUnit.getId()!=0){
            Integer i= courseUnitMapper.updateUnit(courseUnit);
            if(i>0){
                return CommonResult.ok();
            }
        }
        return CommonResult.error(500,"修改失败");
    }

    /**
    * @Author 杨友朋
    * @Description TODO 删除课程
    * @Date 2023/8/21 16:20
    * @Return
    * @param
    */
    @Override
    public CommonResult delUnit(Integer id) {
      if(id!=0&&id!=null){
          Integer i=courseUnitMapper.delUnit(id);
          if(i>0){
              return  CommonResult.ok();
          }
      }
        return CommonResult.error(500,"删除失败");
    }

    /**
     * @Author 杨友朋
     * @Description TODO 单查章节
     * @Date 2023/8/24 9:00
     * @Return
     * @param
     */
    @Override
    public CommonResult selectUnitById(Integer id) {
        CourseUnit courseUnit=courseUnitMapper.selectUnitById(id);
        return CommonResult.ok(courseUnit);
    }

    /**
     * @Author 杨友朋
     * @Description TODO 更改状态
     * @Date 2023/8/24 9:23
     * @Return
     * @param
     */
    @Override
    public CommonResult upSwitch(CourseUnit courseUnit) {
        courseUnit.setUpdateTime(new Date());
       courseUnitMapper.updateSwitch(courseUnit);
        return CommonResult.ok();
    }

    /**
    * @Author MJL
    * @Description todo 将章节进行下架操作
    * @Date 2023/9/2 21:15
    * @Return  * @param null
    */
    @Override
    public CommonResult banned(Integer id) {
        Integer i = courseUnitMapper.banned(id);
        if (i>=1) {
            return CommonResult.ok("下架成功");
        } else {
            return CommonResult.error("下架失败");
        }
    }

    /**
    * @Author MJL
    * @Description todo 将章节进行上架操作
    * @Date 2023/9/2 21:28
    * @Return  * @param null
    */
    @Override
    public CommonResult enables(Integer id) {
        Integer i = courseUnitMapper.enables(id);
        if (i>=1) {
            return CommonResult.ok("上架成功");
        } else {
            return CommonResult.error("下架失败");
        }
    }
}
